<template>
  <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="dialogVisible" width="20%">
    <el-form ref="formItem" :model="formItem" label-width="110px" size="small" :rules="rules">
      <el-row>
        <el-col :span="24">
          <el-form-item label="选择客户" prop="custId">
            <el-select v-model="formItem.custId" clearable filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteMethod">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.custId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="授信类别" prop="creditType">
            <el-select v-model="formItem.creditType" placeholder="请选择">
              <el-option label="散装水泥" :value="1" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="授信方式" prop="creditUnit">
            <el-select v-model="formItem.creditUnit" placeholder="请选择">
              <el-option label="重量" value="1" />
              <el-option label="金额" value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="授信额度" prop="creditQuota">
            <el-input v-model="formItem.creditQuota" style="width:215px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="授信开始时间" prop="startDate">
            <el-date-picker v-model="formItem.startDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="授信结束时间" prop="endDate">
            <el-date-picker v-model="formItem.endDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="选择计量员" prop="userId">
            <el-select v-model="formItem.userId" clearable filterable placeholder="请选择">
              <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit('formItem')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addCredit, updateCredit } from '@/api/credit'
import { getTenCust } from '@/api/cust'
import { getUserList } from '@/api/user'
import _ from 'lodash'
export default {
  data () {
    return {
      isEdit: false,
      dialogVisible: false,
      options: [],
      userList: [],
      formItem: {
        id: '',
        custId: '',
        creditType: '',
        creditUnit: '',
        creditQuota: '',
        startDate: '',
        endDate: '',
        userId: ''
      },
      rules: {
        custId: [{ required: true, message: '请选择客户', trigger: 'blur' }],
        startDate: [{ required: true, message: '请选择授信开始时间', trigger: 'blur' }],
        creditQuota: [{ required: true, message: '请输入', trigger: 'blur' }],
        userId: [{ required: true, message: '请选择授信人', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (row) {
      if (row) {
        this.title = '编辑'
        this.formItem = _.clone(row)
        this.isEdit = true
        this.options = [{ custId: row.custId, name: row.custName }]
        delete this.formItem.updateTime
      } else {
        this.title = '新增'
        this.formItem = {
          custId: '',
          creditType: 1,
          creditUnit: '1',
          creditQuota: '',
          startDate: '',
          endDate: '',
          userId: ''
        }
        this.isEdit = false
      }
      this.dialogVisible = true
      this.queryUserList()
    },
    async remoteMethod (keyword) {
      if (keyword) {
        const res = await getTenCust({ keyword })
        if (res.code === 200) {
          this.options = res.data
        } else {
          this.options = []
        }
      } else {
        this.options = [];
      }
    },
    async queryUserList () {
      const res = await getUserList()
      if (res.code === 200) {
        this.userList = res.data
        this.formItem.userId = this.$store.getters.getUser.userId
      } else {
        this.userList = []
        this.$message.error(res.msg)
      }
    },
    handleSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = this.isEdit ? await updateCredit(this.formItem) : await addCredit(this.formItem)
          if (res.code === 200) {
            this.$emit('ok')
            this.dialogVisible = false
          } else {
            this.$message.error(res.msg)
          }
        }
      })
    }
  }
}
</script>

<style>
</style>